package com.nzf.vote.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nzf.vote.entity.User;
import com.nzf.vote.model.vo.Results;
import com.nzf.vote.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author nzf
 * @since 2022-07-25
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService service;

    @PostMapping("/login")
    public Results login(@RequestBody User user){
        User user1 = service.login(user.getUsername(), user.getPassword());
        return Results.success(user1);
    }
    @GetMapping("getUser/{id}")
    public Results getUser(@PathVariable Integer id){
        return Results.success(service.getById(id));
    }


    @GetMapping("/getUserList")
    public Results getUserList(){
        return Results.success(service.list());
    }
    @GetMapping("/getUserList/{pageIndex}/{size}")
    public Results getUserLists(@PathVariable Integer pageIndex,@PathVariable Integer size){
        Page<User> userPage = new Page<>(pageIndex,size);
        Page<User> page = new LambdaQueryChainWrapper<>(service.getBaseMapper()).page(userPage);
        return Results.success(page);
    }

    @PostMapping("/insertUser")
    public Results insertUser(@RequestBody User user){
        return service.insertUser(user);
    }
    @DeleteMapping("deleteUser/{id}")
    public Results deleteUser(@PathVariable Integer id){
        return Results.success(service.deleteUser(id));
    }
    @PutMapping("updateUser")
    public Results updateUser(@RequestBody User user){
        return Results.success(service.updateById(user));
    }
}
